<?php
require_once APPLICATION_PATH. '/../library/PayPal.php';

class Uuch_PayPal extends PayPal{
    
    private $config;
    public $API_UserName;
    public $API_Password;
    public $API_Signature;
    public $API_Endpoint;
    public $subject;
    public $version;
    public $AUTH_token;
    public $AUTH_signature;
    public $AUTH_timestamp;
    
    function __construct(){
        $this->config = new Zend_Config_Ini(APPLICATION_PATH. '/configs/config.ini',APPLICATION_ENV);
        //$this->debug_response(APPLICATION_PATH.'/logs/debug.txt');
        $this->log_file(APPLICATION_PATH.'/logs/log.txt');
        $this->paypal_host(45, $this->config->paypalhost,443);
        
        $this->API_UserName = '21810665_api1.qq.com';
        $this->API_Password = '5BHVBN3CHQD7J5RW';
        $this->API_Signature = 'AFcWxV21C7fd0v3bYYYRCpSSRl31AhANIct2XrYxa1C3exFSwKc64mOt';
        $this->API_Endpoint = 'https://api-3t.paypal.com/nvp';
        $this->subject = '';
        define('USE_PROXY', false);
        define('PROXY_HOST', '127.0.0.1');
        define('PROXY_PORT', '808');
        $this->PAYPAL_URL = 'https://www.paypal.com/webscr&cmd=_express-checkout&token=';
        $this->version = '65.1';
        define('ACK_SUCCESS', 'SUCCESS');
        define('AUTH_TOKEN', 'SUCCESS');
        define('ACK_SUCCESS_WITH_WARNING', 'SUCCESSWITHWARNING');
// below three are needed if used permissioning
        if (defined('AUTH_TOKEN')) {
            $this->AUTH_token = AUTH_TOKEN;
        }

        if (defined('AUTH_SIGNATURE')) {
            $this->AUTH_signature = AUTH_SIGNATURE;
        }

        if (defined('AUTH_TIMESTAMP')) {
            $this->AUTH_timestamp = AUTH_TIMESTAMP;
        }
        
    }

    public function check_email($email) {
        return strcmp($email , $this->config->account)==0;
    }
    
    public function check_amount($item_no, $amount, $currency) {
        $item = Application_Model_PayBill::load($_GET['pid']);
        return $item->Usmoney == $amount && $currency == 'USD' && $item->Status != Application_Model_PayBill::PAY_STATUS_SUCCESS;
    }
    
    public function check_txn_id($id) {
        return !Application_Model_PayBill::isExistTxnId($id);
    }


    public function status_completed() {
        return TRUE;
    }
    
    public function status_cancel_reverse() {
        parent::status_cancel_reverse();
        return FALSE;
    }
    
    public function status_denied() {
        parent::status_denied();
        return(FALSE);
    }
    
    public function status_failed() {
        parent::status_failed();
        return FALSE;
    }
    
    public function status_pending() {
        parent::status_pending();
        return(FALSE);
    }
    
    public function status_refunded() {
        parent::status_refunded();
        return(FALSE);
    }
    
    public function status_reversed() {
        parent::status_reversed();
        return FALSE;
    }


    public function nvpHeader() {
        global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature, $nvp_Header, $subject, $AUTH_token, $AUTH_signature, $AUTH_timestamp;
        $nvpHeaderStr = "";

        if (defined('AUTH_MODE')) {
            //$AuthMode = "3TOKEN"; //Merchant's API 3-TOKEN Credential is required to make API Call.
            //$AuthMode = "FIRSTPARTY"; //Only merchant Email is required to make EC Calls.
            //$AuthMode = "THIRDPARTY";Partner's API Credential and Merchant Email as Subject are required.
            $AuthMode = "AUTH_MODE";
        } else {

            if ((!empty($this->API_UserName)) && (!empty($this->API_Password)) && (!empty($this->API_Signature)) && (!empty($this->subject))) {
                $this->AuthMode = "THIRDPARTY";
            } else if ((!empty($this->API_UserName)) && (!empty($this->API_Password)) && (!empty($this->API_Signature))) {
                $AuthMode = "3TOKEN";
            } elseif (!empty($this->AUTH_token) && !empty($this->AUTH_signature) && !empty($this->AUTH_timestamp)) {
                $AuthMode = "PERMISSION";
            } elseif (!empty($this->subject)) {
                $AuthMode = "FIRSTPARTY";
            }
        }
        switch ($AuthMode) {

            case "3TOKEN" :
                $nvpHeaderStr = "&PWD=" . urlencode($this->API_Password) . "&USER=" . urlencode($this->API_UserName) . "&SIGNATURE=" . urlencode($this->API_Signature);
                break;
            case "FIRSTPARTY" :
                $nvpHeaderStr = "&SUBJECT=" . urlencode($this->subject);
                break;
            case "THIRDPARTY" :
                $nvpHeaderStr = "&PWD=" . urlencode($this->API_Password) . "&USER=" . urlencode($this->API_UserName) . "&SIGNATURE=" . urlencode($this->API_Signature) . "&SUBJECT=" . urlencode($this->subject);
                break;
            case "PERMISSION" :
                $nvpHeaderStr = formAutorization($this->AUTH_token, $this->AUTH_signature, $this->AUTH_timestamp);
                break;
        }
        return $nvpHeaderStr;
    }

    public function hash_call($methodName, $nvpStr) {
        //declaring of global variables
        global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature, $nvp_Header, $subject, $AUTH_token, $AUTH_signature, $AUTH_timestamp;
        // form header string
        $nvpheader = $this->nvpHeader();
        //setting the curl parameters.
        $ch = curl_init(null);
        curl_setopt($ch, CURLOPT_URL, $this->API_Endpoint);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);

        //turning off the server and peer verification(TrustManager Concept).
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);

        //in case of permission APIs send headers as HTTPheders
        if (!empty($this->AUTH_token) && !empty($this->AUTH_signature) && !empty($this->AUTH_timestamp)) {
            $headers_array[] = "X-PP-AUTHORIZATION: " . $nvpheader;

            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_array);
            curl_setopt($ch, CURLOPT_HEADER, false);
        } else {
            $nvpStr = $nvpheader . $nvpStr;
        }
        //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
        //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php 
        if (USE_PROXY) {
            curl_setopt($ch, CURLOPT_PROXY, PROXY_HOST . ":" . PROXY_PORT);
        }

        //check if version is included in $nvpStr else include the version.
        if (strlen(str_replace('VERSION=', '', strtoupper($nvpStr))) == strlen($nvpStr)) {
            $nvpStr = "&VERSION=" . urlencode($this->version) . $nvpStr;
        }

        $nvpreq = "METHOD=" . urlencode($methodName) . $nvpStr;


        //setting the nvpreq as POST FIELD to curl
        curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

        //getting response from server
        $response = curl_exec($ch);

        //convrting NVPResponse to an Associative Array
        $nvpResArray = $this->deformatNVP($response);
        $nvpReqArray = $this->deformatNVP($nvpreq);
        $_SESSION['nvpReqArray'] = $nvpReqArray;

        if (curl_errno($ch)) {
            // moving to display page to display curl errors
            $_SESSION['curl_error_no'] = curl_errno($ch);
            $_SESSION['curl_error_msg'] = curl_error($ch);
            $location = "APIError.php";
            header("Location: $location");
        } else {
            //closing the curl
            curl_close($ch);
        }

        return $nvpResArray;
    }

    public function deformatNVP($nvpstr) {

        $intial = 0;
        $nvpArray = array();


        while (strlen($nvpstr)) {
            //postion of Key
            $keypos = strpos($nvpstr, '=');
            //position of value
            $valuepos = strpos($nvpstr, '&') ? strpos($nvpstr, '&') : strlen($nvpstr);

            /* getting the Key and Value values and storing in a Associative Array */
            $keyval = substr($nvpstr, $intial, $keypos);
            $valval = substr($nvpstr, $keypos + 1, $valuepos - $keypos - 1);
            //decoding the respose
            $nvpArray[urldecode($keyval)] = urldecode($valval);
            $nvpstr = substr($nvpstr, $valuepos + 1, strlen($nvpstr));
        }

        return $nvpArray;
    }

    public function formAutorization($auth_token, $auth_signature, $auth_timestamp) {
        echo $authString = "token=" . $auth_token . ",signature=" . $auth_signature . ",timestamp=" . $auth_timestamp;
        return $authString;
    }
}
